<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MongoDB架构与核心原理深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card {
            transition: all 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
            background-color: #1e293b;
            color: #f8fafc;
            border-radius: 0.5rem;
        }
        .section-divider {
            position: relative;
            height: 80px;
            overflow: hidden;
        }
        .section-divider svg {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        .tooltip {
            position: relative;
            display: inline-block;
        }
        .tooltip .tooltiptext {
            visibility: hidden;
            width: 200px;
            background-color: #1e293b;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 8px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            transform: translateX(-50%);
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 14px;
        }
        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero text-white py-20 md:py-32 px-4 sm:px-6 lg:px-8">
        <div class="max-w-7xl mx-auto">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-6xl font-bold mb-6 animate-fade-in-down">MongoDB架构与核心原理</h1>
                <p class="text-xl md:text-2xl max-w-3xl mb-8 opacity-90">深入解析现代NoSQL数据库的设计哲学与实现机制</p>
                <div class="flex space-x-4">
                    <a href="#architecture" class="bg-white text-indigo-600 px-6 py-3 rounded-full font-medium hover:bg-indigo-50 transition duration-300">
                        探索架构
                    </a>
                    <a href="#visualization" class="border-2 border-white px-6 py-3 rounded-full font-medium hover:bg-white hover:text-indigo-600 transition duration-300">
                        可视化解析
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Architecture Section -->
    <section id="architecture" class="py-16 px-4 sm:px-6 lg:px-8 bg-white">
        <div class="max-w-7xl mx-auto">
            <div class="text-center mb-16">
                <span class="inline-block px-4 py-2 bg-indigo-100 text-indigo-800 rounded-full text-sm font-semibold mb-4">核心架构</span>
                <h2 class="text-3xl md:text-4xl font-bold mb-4">MongoDB服务器架构详解</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">现代分布式数据库系统的精妙设计与实现原理</p>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8 mb-16">
                <!-- Process Card -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-microchip"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">进程与组件</h3>
                    <ul class="space-y-2 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>mongod 主数据库进程</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>mongos 路由进程</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>配置服务器进程</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                            <span>MongoDB Shell</span>
                        </li>
                    </ul>
                </div>

                <!-- Internal Architecture Card -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-sitemap"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">服务器内部架构</h3>
                    <ul class="space-y-2 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-cog text-indigo-500 mt-1 mr-2"></i>
                            <span>查询解析器</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-cog text-indigo-500 mt-1 mr-2"></i>
                            <span>查询优化器</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-cog text-indigo-500 mt-1 mr-2"></i>
                            <span>执行引擎</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-cog text-indigo-500 mt-1 mr-2"></i>
                            <span>存储引擎接口</span>
                        </li>
                    </ul>
                </div>

                <!-- Request Flow Card -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">请求处理流程</h3>
                    <ol class="space-y-2 text-gray-600 list-decimal list-inside">
                        <li>客户端发送请求</li>
                        <li>连接处理</li>
                        <li>请求解析</li>
                        <li>查询优化</li>
                        <li>执行计划生成</li>
                        <li>数据访问</li>
                        <li>结果返回</li>
                    </ol>
                </div>

                <!-- Concurrency Card -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-users-cog"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">并发模型</h3>
                    <ul class="space-y-2 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-indigo-500 mt-1 mr-2"></i>
                            <span>多线程架构</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-indigo-500 mt-1 mr-2"></i>
                            <span>读写锁机制</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-indigo-500 mt-1 mr-2"></i>
                            <span>文档级并发控制</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-indigo-500 mt-1 mr-2"></i>
                            <span>内存管理</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="mt-16">
                <div class="flex items-center mb-8">
                    <div class="w-1 h-10 bg-indigo-600 mr-4"></div>
                    <h3 class="text-2xl font-bold">存储引擎深度解析</h3>
                </div>

                <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                    <div>
                        <h4 class="text-xl font-semibold mb-4 text-gray-800">WiredTiger引擎特性</h4>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <span class="inline-flex items-center justify-center h-6 w-6 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3 flex-shrink-0">1</span>
                                <span>文档级并发控制</span>
                            </li>
                            <li class="flex items-start">
                                <span class="inline-flex items-center justify-center h-6 w-6 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3 flex-shrink-0">2</span>
                                <span>压缩存储</span>
                            </li>
                            <li class="flex items-start">
                                <span class="inline-flex items-center justify-center h-6 w-6 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3 flex-shrink-0">3</span>
                                <span>检查点机制</span>
                            </li>
                            <li class="flex items-start">
                                <span class="inline-flex items-center justify-center h-6 w-6 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3 flex-shrink-0">4</span>
                                <span>日志记录</span>
                            </li>
                            <li class="flex items-start">
                                <span class="inline-flex items-center justify-center h-6 w-6 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3 flex-shrink-0">5</span>
                                <span>内存管理</span>
                            </li>
                        </ul>
                    </div>

                    <div>
                        <h4 class="text-xl font-semibold mb-4 text-gray-800">配置示例</h4>
                        <div class="code-block p-6 rounded-xl">
                            <pre class="text-green-400 overflow-x-auto"><code># mongod.conf
storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      journalCompressor: snappy
    collectionConfig:
      blockCompressor: snappy</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Divider -->
    <div class="section-divider bg-white">
        <svg viewBox="0 0 1200 120" preserveAspectRatio="none">
            <path d="M0,0V46.29c47.79,22.2,103.59,32.17,158,28,70.36-5.37,136.33-33.31,206.8-37.5C438.64,32.43,512.34,53.67,583,72.05c69.27,18,138.3,24.88,209.4,13.08,36.15-6,69.85-17.84,104.45-29.34C989.49,25,1113-14.29,1200,52.47V0Z" opacity=".25" fill="#818cf8"></path>
            <path d="M0,0V15.81C13,36.92,27.64,56.86,47.69,72.05,99.41,111.27,165,111,224.58,91.58c31.15-10.15,60.09-26.07,89.67-39.8,40.92-19,84.73-46,130.83-49.67,36.26-2.85,70.9,9.42,98.6,31.56,31.77,25.39,62.32,62,103.63,73,40.44,10.79,81.35-6.69,119.13-24.28s75.16-39,116.92-43.05c59.73-5.85,113.28,22.88,168.9,38.84,30.2,8.66,59,6.17,87.09-7.5,22.43-10.89,48-26.93,60.65-49.24V0Z" opacity=".5" fill="#818cf8"></path>
            <path d="M0,0V5.63C149.93,59,314.09,71.32,475.83,42.57c43-7.64,84.23-20.12,127.61-26.46,59-8.63,112.48,12.24,165.56,35.4C827.93,77.22,886,95.24,951.2,90c86.53-7,172.46-45.71,248.8-84.81V0Z" fill="#818cf8"></path>
        </svg>
    </div>

    <!-- Indexes Section -->
    <section class="py-16 px-4 sm:px-6 lg:px-8 bg-indigo-50">
        <div class="max-w-7xl mx-auto">
            <div class="text-center mb-16">
                <span class="inline-block px-4 py-2 bg-indigo-100 text-indigo-800 rounded-full text-sm font-semibold mb-4">索引机制</span>
                <h2 class="text-3xl md:text-4xl font-bold mb-4">索引类型与优化策略</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">高性能查询背后的秘密武器</p>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                <!-- Single Field Index -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                            <i class="fas fa-map-pin"></i>
                        </div>
                        <h3 class="text-xl font-bold">单字段索引</h3>
                    </div>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <pre class="text-green-400 overflow-x-auto"><code>// 创建单字段索引
db.users.createIndex({ "email": 1 })

// 创建唯一索引
db.users.createIndex({ "email": 1 }, 
  { unique: true })</code></pre>
                    </div>
                    <p class="text-gray-600">最基本的索引类型，适用于精确匹配查询</p>
                </div>

                <!-- Compound Index -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                            <i class="fas fa-layer-group"></i>
                        </div>
                        <h3 class="text-xl font-bold">复合索引</h3>
                    </div>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <pre class="text-green-400 overflow-x-auto"><code>// 创建复合索引
db.users.createIndex({ 
  "lastName": 1, 
  "firstName": 1 
})

// 复合唯一索引
db.users.createIndex(
  { "email": 1, "domain": 1 },
  { unique: true }
)</code></pre>
                    </div>
                    <p class="text-gray-600">多字段组合索引，支持高效的多条件查询</p>
                </div>

                <!-- Multikey Index -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                            <i class="fas fa-braille"></i>
                        </div>
                        <h3 class="text-xl font-bold">多键索引</h3>
                    </div>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <pre class="text-green-400 overflow-x-auto"><code>// 数组字段索引
db.users.createIndex({ "tags": 1 })

// 嵌套数组索引
db.users.createIndex({ 
  "addresses.zip": 1 
})</code></pre>
                    </div>
                    <p class="text-gray-600">为数组字段中的每个元素创建索引</p>
                </div>

                <!-- Text Index -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                            <i class="fas fa-font"></i>
                        </div>
                        <h3 class="text-xl font-bold">文本索引</h3>
                    </div>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <pre class="text-green-400 overflow-x-auto"><code>// 创建文本索引
db.articles.createIndex(
  { 
    "title": "text", 
    "content": "text" 
  },
  { 
    weights: { 
      title: 10, 
      content: 5 
    } 
  }
)

// 文本搜索
db.articles.find({
  $text: { 
    $search: "mongodb tutorial" 
  }
})</code></pre>
                    </div>
                    <p class="text-gray-600">支持全文搜索功能</p>
                </div>

                <!-- Geospatial Index -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                            <i class="fas fa-globe-americas"></i>
                        </div>
                        <h3 class="text-xl font-bold">地理空间索引</h3>
                    </div>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <pre class="text-green-400 overflow-x-auto"><code>// 2d索引
db.places.createIndex({ 
  "location": "2d" 
})

// 2dsphere索引
db.places.createIndex({ 
  "location": "2dsphere" 
})

// 地理空间查询
db.places.find({
  location: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [longitude, latitude]
      },
      $maxDistance: 1000
    }
  }
})</code></pre>
                    </div>
                    <p class="text-gray-600">支持地理位置查询和位置服务</p>
                </div>

                <!-- Index Properties -->
                <div class="card bg-white rounded-xl shadow-lg p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                            <i class="fas fa-cog"></i>
                        </div>
                        <h3 class="text-xl font-bold">索引属性</h3>
                    </div>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <pre class="text-green-400 overflow-x-auto"><code>// TTL索引
db.logs.createIndex(
  { "createdAt": 1 },
  { expireAfterSeconds: 3600 }
)

// 部分索引
db.users.createIndex(
  { "email": 1 },
  { 
    partialFilterExpression: { 
      status: "active" 
    } 
  }
)

// 稀疏索引
db.users.createIndex(
  { "email": 1 },
  { sparse: true }
)</code></pre>
                    </div>
                    <p class="text-gray-600">特殊用途的高级索引类型</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Aggregation Section -->
    <section class="py-16 px-4 sm:px-6 lg:px-8 bg-white">
        <div class="max-w-7xl mx-auto">
            <div class="text-center mb-16">
                <span class="inline-block px-4 py-2 bg-indigo-100 text-indigo-800 rounded-full text-sm font-semibold mb-4">数据处理</span>
                <h2 class="text-3xl md:text-4xl font-bold mb-4">聚合框架全解析</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">灵活强大的数据分析工具</p>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-bold mb-6">聚合管道示例</h3>
                    <div class="code-block p-6 rounded-xl mb-8">
                        <pre class="text-green-400 overflow-x-auto"><code>db.orders.aggregate([
  // 匹配阶段
  { $match: { status: "completed" } },
  
  // 分组阶段
  { $group: {
    _id: "$customerId",
    totalAmount: { $sum: "$amount" }
  }},
  
  // 排序阶段
  { $sort: { totalAmount: -1 } },
  
  // 限制阶段
  { $limit: 10 }
])</code></pre>
                    </div>

                    <h3 class="text-2xl font-bold mb-6">核心管道操作符</h3>
                    
                    <div class="mb-6">
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <span class="inline-flex items-center justify-center h-8 w-8 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3">$</span>
                            <span>match</span>
                        </h4>
                        <div class="code-block p-4 rounded-lg">
                            <pre class="text-green-400 overflow-x-auto"><code>{ $match: {
  age: { $gt: 18 },
  status: "active"
}}</code></pre>
                        </div>
                        <p class="text-gray-600 mt-2">筛选文档，类似于find()方法</p>
                    </div>

                    <div class="mb-6">
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <span class="inline-flex items-center justify-center h-8 w-8 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3">$</span>
                            <span>project</span>
                        </h4>
                        <div class="code-block p-4 rounded-lg">
                            <pre class="text-green-400 overflow-x-auto"><code>{ $project: {
  name: 1,
  age: 1,
  fullName: { $concat: ["$firstName", " ", "$lastName"] }
}}</code></pre>
                        </div>
                        <p class="text-gray-600 mt-2">控制输出文档的字段</p>
                    </div>

                    <div class="mb-6">
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <span class="inline-flex items-center justify-center h-8 w-8 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3">$</span>
                            <span>group</span>
                        </h4>
                        <div class="code-block p-4 rounded-lg">
                            <pre class="text-green-400 overflow-x-auto"><code>{ $group: {
  _id: "$department",
  avgSalary: { $avg: "$salary" },
  totalEmployees: { $sum: 1 }
}}</code></pre>
                        </div>
                        <p class="text-gray-600 mt-2">按指定字段分组并计算聚合值</p>
                    </div>
                </div>

                <div>
                    <div class="mb-8">
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <span class="inline-flex items-center justify-center h-8 w-8 rounded-full bg-indigo-100 text-indigo-800 font-bold mr-3">$</span>
                            <span>lookup</span>
                        </h4>
                        <div class="code-block p-4 rounded-lg">
                            <pre class="text-green-400 overflow-x-auto"><code>{ $lookup: {
  from: "orders",
  localField: "_id",
  foreignField: "customerId",
  as: "orders"
}}</code></pre>
                        </div>
                        <p class="text-gray-600 mt-2">执行类似SQL中的左外连接</p>
                    </div>

                    <h3 class="text-2xl font-bold mb-6">聚合表达式</h3>
                    
                    <div class="mb-6">
                        <h4 class="text-xl font-semibold mb-3">条件表达式</h4>
                        <div class="code-block p-4 rounded-lg">
                            <pre class="text-green-400 overflow-x-auto"><code>// 条件表达式
{ $cond: {
  if: { $gt: ["$age", 18] },
  then: "adult",
  else: "minor"
}}</code></pre>
                        </div>
                        <p class="text-gray-600 mt-2">根据条件返回不同的值</p>
                    </div>

                    <div class="mb-6">
                        <h4 class="text-xl font-semibold mb-3">数组表达式</h4>
                        <div class="code-block p-4 rounded-lg">
                            <pre class="text-green-400 overflow-x-auto"><code>// 数组表达式
{ $map: {
  input: "$scores",
  as: "score",
  in: { $multiply: ["$$score", 2] }
}}</code></pre>
                        </div>
                        <p class="text-gray-600 mt-2">对数组中的每个元素进行处理</p>
                    </div>

                    <div class="bg-indigo-50 p-6 rounded-xl border border-indigo-100">
                        <h4 class="text-xl font-semibold mb-3 flex items-center text-indigo-800">
                            <i class="fas fa-lightbulb mr-2"></i>
                            <span>专家提示</span>
                        </h4>
                        <p class="text-gray-700">聚合管道可以包含多个阶段，每个阶段都会对输入文档进行处理并输出新文档。阶段可以重复使用，也可以按需组合，这使得聚合框架非常灵活强大。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Transactions Section -->
    <section class="py-16 px-4 sm:px-6 lg:px-8 bg-gray-50">
        <div class="max-w-7xl mx-auto">
            <div class="text-center mb-16">
                <span class="inline-block px-4 py-2 bg-indigo-100 text-indigo-800 rounded-full text-sm font-semibold mb-4">事务处理</span>
                <h2 class="text-3xl md:text-4xl font-bold mb-4">事务支持与ACID特性</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">确保数据一致性的关键机制</p>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-bold mb-6">事务模型</h3>
                    <div class="code-block p-6 rounded-xl mb-8">
                        <pre class="text-green-400 overflow-x-auto"><code>// 开始事务
session = db.getMongo().startSession()
session.startTransaction()

try {
  // 事务操作
  db.users.updateOne(
    { _id: userId },
    { $inc: { balance: -100 } },
    { session }
  )
  
  db.orders.insertOne(
    { userId: userId, amount: 100 },
    { session }
  )
  
  // 提交事务
  session.commitTransaction()
} catch (error) {
  // 回滚事务
  session.abortTransaction()
} finally {
  session.endSession()
}</code></pre>
                    </div>

                    <h3 class="text-2xl font-bold mb-6">事务隔离级别</h3>
                    <div class="grid grid-cols-2 gap-4">
                        <div class="bg-white p-4 rounded-lg shadow-sm">
                            <div class="flex items-center mb-2">
                                <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-2">
                                    <i class="fas fa-eye"></i>
                                </div>
                                <h4 class="font-semibold">读未提交</h4>
                            </div>
                            <p class="text-sm text-gray-600">可能读取到未提交的数据</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg shadow-sm">
                            <div class="flex items-center mb-2">
                                <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-2">
                                    <i class="fas fa-check-circle"></i>
                                </div>
                                <h4 class="font-semibold">读已提交</h4>
                            </div>
                            <p class="text-sm text-gray-600">只读取已提交的数据</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg shadow-sm">
                            <div class="flex items-center mb-2">
                                <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-2">
                                    <i class="fas fa-redo"></i>
                                </div>
                                <h4 class="font-semibold">可重复读</h4>
                            </div>
                            <p class="text-sm text-gray-600">事务内多次读取结果一致</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg shadow-sm">
                            <div class="flex items-center mb-2">
                                <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-2">
                                    <i class="fas fa-lock"></i>
                                </div>
                                <h4 class="font-semibold">串行化</h4>
                            </div>
                            <p class="text-sm text-gray-600">最高隔离级别，完全串行执行</p>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-6">分布式事务</h3>
                    <div class="code-block p-6 rounded-xl mb-8">
                        <pre class="text-green-400 overflow-x-auto"><code>// 跨分片事务
session.startTransaction({
  readConcern: { level: "snapshot" },
  writeConcern: { w: "majority" }
})</code></pre>
                    </div>

                    <h3 class="text-2xl font-bold mb-6">事务性能优化</h3>
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                <i class="fas fa-expand-arrows-alt"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold">事务大小控制</h4>
                                <p class="text-gray-600">限制事务中操作的数量和数据量，避免长时间运行</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                <i class="fas fa-recycle"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold">会话复用</h4>
                                <p class="text-gray-600">复用会话对象而非频繁创建，减少开销</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                <i class="fas fa-sliders-h"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold">读写关注点设置</h4>
                                <p class="text-gray-600">根据业务需求调整读写关注级别</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 mr-4">
                                <i class="fas fa-hourglass-half"></i>
                            </div>
                            <div>
                                <h4 class="font-semibold">超时设置</h4>
                                <p class="text-gray-600">合理设置事务超时时间，避免资源长时间占用</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Visualization Section -->
    <section id="visualization" class="py-16 px-4 sm:px-6 lg:px-8 bg-white">
        <div class="max-w-7xl mx-auto">
            <div class="text-center mb-16">
                <span class="inline-block px-4 py-2 bg-indigo-100 text-indigo-800 rounded-full text-sm font-semibold mb-4">可视化</span>
                <h2 class="text-3xl md:text-4xl font-bold mb-4">MongoDB架构可视化</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">通过交互式图表深入理解核心概念</p>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-16">
                <div class="bg-gray-50 p-6 rounded-xl border border-gray-200">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-diagram-project mr-2 text-indigo-600"></i>
                        <span>MongoDB组件关系图</span>
                    </h3>
                    <div class="mermaid">
                        graph TD
                            A[mongod] -->|主数据库进程| B[存储引擎]
                            A --> C[查询解析器]
                            A --> D[查询优化器]
                            A --> E[执行引擎]
                            F[mongos] -->|路由进程| G[分片集群]
                            F --> H[配置服务器]
                            I[客户端] -->|连接| A
                            I -->|连接| F
                            J[管理工具] -->|监控| A
                            J -->|管理| F
                    </div>
                </div>

                <div class="bg-gray-50 p-6 rounded-xl border border-gray-200">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-sitemap mr-2 text-indigo-600"></i>
                        <span>索引类型思维导图</span>
                    </h3>
                    <div class="mermaid">
                        mindmap
                          root((索引类型))
                            单字段索引
                              升序索引
                              降序索引
                              唯一索引
                            复合索引
                              多字段组合
                              排序方向
                            多键索引
                              数组索引
                              嵌套文档索引
                            特殊索引
                              文本索引
                              地理空间索引
                              TTL索引
                              部分索引
                              稀疏索引
                    </div>
                </div>
            </div>

            <div class="bg-gray-50 p-6 rounded-xl border border-gray-200">
                <h3 class="text-xl font-bold mb-4 flex items-center">
                    <i class="fas fa-random mr-2 text-indigo-600"></i>
                    <span>请求处理流程图</span>
                </h3>
                <div class="mermaid">
                    flowchart LR
                        A[客户端请求] --> B[连接处理]
                        B --> C[请求解析]
                        C --> D{查询?}
                        D -->|是| E[查询优化]
                        D -->|否| F[直接执行]
                        E --> G[执行计划生成]
                        G --> H[数据访问]
                        H --> I[结果返回]
                        F --> I
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12 px-4 sm:px-6 lg:px-8">
        <div class="max-w-7xl mx-auto">
            <div class="flex flex-col items-center">
                <div class="text-xl font-bold text-white mb-2">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition duration-300">http://www.yuque.com/jtostring</a>
                <div class="mt-6 flex space-x-6">
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-github text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-twitter text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fab fa-linkedin text-xl"></i>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            themeCSS: `
                .node rect {
                    fill: #e0e7ff;
                    stroke: #4f46e5;
                    stroke-width: 2px;
                }
                .edgePath path {
                    stroke: #4f46e5;
                    stroke-width: 2px;
                }
                .cluster rect {
                    fill: #f5f3ff;
                    stroke: #7c3aed;
                    stroke-width: 2px;
                    rx: 5;
                    ry: 5;
                }
            `
        });
    </script>
</body>
</html>